package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.ExhibitionSettlementInvoicingMapper;
import com.eastfair.venueservice.dto.ExhibitionSettlementInvoicingDTO;
import com.eastfair.venueservice.entity.ExhibitionSettlement;
import com.eastfair.venueservice.entity.ExhibitionSettlementInvoicing;
import com.eastfair.venueservice.entity.ExhibitionSettlementRefund;
import com.eastfair.venueservice.enumeration.ExhibitionSettlementInvoicingStatusEnum;
import com.eastfair.venueservice.service.ExhibitionSettlementInvoicingService;
import com.eastfair.venueservice.service.ExhibitionSettlementService;
import com.eastfair.venueservice.vo.ExhibitionSettlementInvoicingVO;
import com.eastfair.venueservice.vo.ExhibitionSettlementRefundVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 结算开票记录
 * </p>
 *
 * @author linan
 * @date 2023-02-01
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionSettlementInvoicingServiceImpl extends SuperServiceImpl<ExhibitionSettlementInvoicingMapper, ExhibitionSettlementInvoicing> implements ExhibitionSettlementInvoicingService {

    @Resource
    private ExhibitionSettlementService exhibitionSettlementService;


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionSettlementInvoicing> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ExhibitionSettlementInvoicing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ExhibitionSettlementInvoicing> list(ExhibitionSettlementInvoicingDTO modelDTO, List<Long> ids) {
        return ((ExhibitionSettlementInvoicingService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'settlementNo', #args[0]?.settlementNo}," +
                            "{'invoicedAmount', #args[0]?.invoicedAmount}," +
                            "{'invoiceType', #args[0]?.invoiceType}," +
                            "{'companyName', #args[0]?.companyName}," +
                            "{'taxpayerIdNo', #args[0]?.taxpayerIdNo}," +
                            "{'companyAddress', #args[0]?.companyAddress}," +
                            "{'tel', #args[0]?.tel}," +
                            "{'bankName', #args[0]?.bankName}," +
                            "{'bankAccountNum', #args[0]?.bankAccountNum}," +
                            "{'phone', #args[0]?.phone}," +
                            "{'mail', #args[0]?.mail}," +
                            "{'invoicingStatus', #args[0]?.invoicingStatus}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ExhibitionSettlementInvoicing> listOfCache(ExhibitionSettlementInvoicingDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<ExhibitionSettlementInvoicing> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSettlementNo()), ExhibitionSettlementInvoicing::getSettlementNo, modelDTO.getSettlementNo());
            queryWrapper.lambda().eq(modelDTO.getInvoicedAmount() != null, ExhibitionSettlementInvoicing::getInvoicedAmount, modelDTO.getInvoicedAmount());
            queryWrapper.lambda().eq(Objects.nonNull(modelDTO.getInvoiceType()), ExhibitionSettlementInvoicing::getInvoiceType, modelDTO.getInvoiceType());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCompanyName()), ExhibitionSettlementInvoicing::getCompanyName, modelDTO.getCompanyName());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getTaxpayerIdNo()), ExhibitionSettlementInvoicing::getTaxpayerIdNo, modelDTO.getTaxpayerIdNo());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getCompanyAddress()), ExhibitionSettlementInvoicing::getCompanyAddress, modelDTO.getCompanyAddress());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getTel()), ExhibitionSettlementInvoicing::getTel, modelDTO.getTel());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getBankName()), ExhibitionSettlementInvoicing::getBankName, modelDTO.getBankName());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getBankAccountNum()), ExhibitionSettlementInvoicing::getBankAccountNum, modelDTO.getBankAccountNum());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPhone()), ExhibitionSettlementInvoicing::getPhone, modelDTO.getPhone());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getMail()), ExhibitionSettlementInvoicing::getMail, modelDTO.getMail());
            queryWrapper.lambda().eq(Objects.nonNull(modelDTO.getInvoicingStatus()), ExhibitionSettlementInvoicing::getInvoicingStatus, modelDTO.getInvoicingStatus());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ExhibitionSettlementInvoicing::getId, ids);
        }
        queryWrapper.lambda().eq(ExhibitionSettlementInvoicing::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ExhibitionSettlementInvoicing> modelList = list(queryWrapper);
        return modelList;
    }

    @Override
    public Boolean saveInvoicingRecord(ExhibitionSettlementInvoicingDTO dto) {
        ExhibitionSettlementInvoicing exhibitionSettlementInvoicing = BeanUtil.copyProperties(dto, ExhibitionSettlementInvoicing.class);
        exhibitionSettlementInvoicing.setInvoicingStatus(ExhibitionSettlementInvoicingStatusEnum.REQUESTED);
        // 更新结算记录
        exhibitionSettlementService.update(new UpdateWrapper<ExhibitionSettlement>().lambda()
                .eq(ExhibitionSettlement::getIsDeleted,BusinessConstant.NO)
                .eq(ExhibitionSettlement::getSettlementNo,dto.getSettlementNo())
                .set(ExhibitionSettlement::getInvoicingStatus,ExhibitionSettlementInvoicingStatusEnum.REQUESTED));
        return save(exhibitionSettlementInvoicing);
    }

    @Override
    public Boolean updateInvoicingRecord(ExhibitionSettlementInvoicingDTO dto) {
        ExhibitionSettlementInvoicing exhibitionSettlementInvoicing = BeanUtil.copyProperties(dto, ExhibitionSettlementInvoicing.class);
        // 更新结算记录
        exhibitionSettlementService.update(new UpdateWrapper<ExhibitionSettlement>().lambda()
                .eq(ExhibitionSettlement::getIsDeleted,BusinessConstant.NO)
                .eq(ExhibitionSettlement::getSettlementNo,dto.getSettlementNo())
                .set(ExhibitionSettlement::getInvoicingStatus,exhibitionSettlementInvoicing.getInvoicingStatus()));
        return updateById(exhibitionSettlementInvoicing);
    }

    @Override
    public ExhibitionSettlementInvoicingVO queryInvoicingRecord(ExhibitionSettlementInvoicingDTO dto) {
        ExhibitionSettlementInvoicing settlementInvoicing = getOne(new UpdateWrapper<ExhibitionSettlementInvoicing>().lambda().eq(ExhibitionSettlementInvoicing::getIsDeleted, BusinessConstant.NO)
                .eq(ExhibitionSettlementInvoicing::getSettlementNo, dto.getSettlementNo()));
        if(Objects.isNull(settlementInvoicing)){
            return null;
        }else {
            return BeanUtil.copyProperties(settlementInvoicing, ExhibitionSettlementInvoicingVO.class);
        }
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionSettlementInvoicingVO doToVo(ExhibitionSettlementInvoicing model) {
        ExhibitionSettlementInvoicingVO modelVo = new ExhibitionSettlementInvoicingVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ExhibitionSettlementInvoicingVO> doToVos(List<ExhibitionSettlementInvoicing> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ExhibitionSettlementInvoicingVO> result = new ArrayList<>();
        for (ExhibitionSettlementInvoicing model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
